package com.lysj.bhp.util.random;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FormatUtil {

    public static Date stringTodates(String string) throws Exception {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.parse(string);
    }

    /**
     * 输入:时间(Date) 输出格式:yyyy-MM-dd HH:mm:ss
     */
    public static String dateToString(Date date) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * 输入:时间(Date) 输出格式: yyyy-MM-dd
     */
    public static String dateToString2(Date date) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 输入:时间(Date) 输出格式:HH:mm:ss
     */
    public static String dateToString3(Date date) {
        DateFormat format = new SimpleDateFormat("HH:mm:ss");
        return format.format(date);
    }

    /**
     * 将HH:mm字符串格式的时间转化成Date类型
     *
     * @param HHmm
     * @return Date
     * @throws Exception
     */
    public static Date stringToTime(String HHmm) throws Exception {
        DateFormat format = new SimpleDateFormat("HH:mm");
        return format.parse(HHmm);
    }

    /**
     * 输入格式:yyyy-MM-dd HH:mm:ss 输出:时间(Date)
     */
    public static Date stringTodate(String string) throws Exception {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(string);
    }

    /**
     * 输入格式:yyyy-MM-dd 输出:时间(Date)
     */
    public static Date stringTodate2(String string) throws Exception {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.parse(string);
    }

    /**
     * 根据Calendar对象中定义的静态变量返回对应的中文名称
     *
     * @param week (1~7)
     * @return
     */
    public static String getWeekName(int week) {
        switch (week) {
            case Calendar.SUNDAY: // 1
                return "周日";
            case Calendar.MONDAY: // 2
                return "周一";
            case Calendar.TUESDAY: // 3
                return "周二";
            case Calendar.WEDNESDAY: // 4
                return "周三";
            case Calendar.THURSDAY: // 5
                return "周四";
            case Calendar.FRIDAY: // 6
                return "周五";
            case Calendar.SATURDAY: // 7
                return "周六";
            default:
                return "";
        }
    }

    // 输出时间
    public static void showDate(Date date) {
        try {
            System.out.println("FormatUtil.showDate():" + dateToString(date));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 对日期+"23:59:59"
    public static Date getDayEndDay(Date day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 对日期取"0:0:0"
    public static Date getDayStartDay(Date day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 获取昨天的 "23:59:59"
    public static Date getEndYesterday(Date day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, -1);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 对日期取"17:0:0"
    public static Date getDaySeventeDay(Date day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 17);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 获取今天 "0:0:0"
    public static Date getStartToday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 获取昨天"0:0:0"
    public static Date getStartYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 判断一个时间是否在两个时间之间
     *
     * @param currentDate
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean isBetweenTime(Date currentDate, Date startDate, Date endDate) {
        if (currentDate == null) {
            return false;
        }
        if (startDate == null && endDate == null) {
            return false;
        }

        if (startDate == null) {
            if (currentDate.getTime() < getDayEndDay(endDate).getTime()) {
                return true;
            }
        }
        if (endDate == null) {
            if (currentDate.getTime() > startDate.getTime()) {
                return true;
            }
        }
        if (currentDate.getTime() > startDate.getTime() && currentDate.getTime() < getDayEndDay(endDate).getTime()) {
            return true;
        }

        return false;
    }

    /**
     * 在今天加上month个月份
     *
     * @param month
     * @return
     */
    public static Date addMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        if (month > 0) {
            calendar.add(Calendar.MONTH, month);
        }
        return calendar.getTime();
    }

    /**
     * @author zk
     * @date 2018/1/16 15:22
     * @Description 某个日期加减count天
     */
    public static Date addDay(Date date, int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, count);
        return calendar.getTime();
    }


    /**
     * 现在时间的秒值
     */
    public static String nowTimeStampSecond() {
        return String.valueOf(System.currentTimeMillis() / 1000);
    }

    /**
     * 现在时间的秒值
     */
    public static String nextMonthTimeStampSecond() {
        Date nextMonth = addMonth(1);// 下个月的时间
        return String.valueOf(nextMonth.getTime() / 1000);
    }

    /*
     * 作者：张文键
     * 自然月增加 参数 1.初始日期 格式 yyyy--MM-dd 2.推算几个月
     * */
    public static Date getNaturalMonth(String InceptionDate, int addMouth) throws Exception {
        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(InceptionDate);//初始日期
        } catch (Exception e) {
        }
        c.setTime(date);//设置日历时间
        c.add(Calendar.MONTH, addMouth);//在日历的月份上增加 addMouth
        return (stringTodate2(sdf.format(c.getTime())));//得到 addMouth 月后的日期
    }

    /*
     * 作者：张文键
     * 自然月增加 参数 1.初始日期 类型 Date 2.推算几个月
     * */
    public static Date getNaturalMonth(Date InceptionDate, int addMouth) throws Exception {
        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(dateToString2(InceptionDate));//初始日期
        } catch (Exception e) {
        }
        c.setTime(date);//设置日历时间
        c.add(Calendar.MONTH, addMouth);//在日历的月份上增加 addMouth
        return (stringTodate2(sdf.format(c.getTime())));//得到 addMouth 月后的日期
    }


    /**
     * @author drj
     * @date 2018/8/24 10:29
     * @Description:得到两个日期相差的天数
     */
    public static long getDifferDays(Date beginDate, Date endDate) {
        long oneDay = 24 * 60 * 60 * 1000; //一天的时间搓
        long beginTime = beginDate.getTime();
        long endTime = endDate.getTime();
        long diff = endTime - beginTime; //用户锁期时间与当前时间的差值
        long remainder = diff % oneDay;
        if (remainder == 0) {
            return (diff / oneDay);
        } else {
            return (diff / oneDay) + 1;
        }

    }


}
